//
//  ZJCustomButton.m
//  IconfontDemo
//
//  Created by ZJ on 2018/7/20.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ZJCustomButton.h"
#import "UIButton+ZJIconfont.h"
#import "UILabel+ZJIconfont.h"

@implementation ZJCustomButton

+ (UILabel *)createLableWithFrame:(CGRect)frame text:(NSString *)text {
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = text;
    label.font = [UIFont systemFontOfSize:12];
    
    return label;
}

- (UIView *)initCustomButtonWithFrame:(CGRect)frame unicode:(NSString *)code title:(NSString *)title target:(nullable id)target action:(SEL)action {
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat x = 0;
        CGFloat y = 0;
        CGFloat width = frame.size.width;
        CGFloat btnHeight = frame.size.height * 0.6;
        CGFloat labelHeight = frame.size.height - btnHeight - 2;
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, btnHeight) unicode:code titleColor:[UIColor redColor]];
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = [ZJCustomButton createLableWithFrame:CGRectMake(x, y + btnHeight + 2, width, labelHeight) text:title];
        
        [self addSubview:button];
        [self addSubview:label];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
        [self addGestureRecognizer:tap];
    }
    
    return self;
}

@end
